import Vue from 'vue'
import Router from 'vue-router'
import {menuRouterMap} from '@/resource/menu/menu'

Vue.use(Router);

const vueRouter = new Router({
    // mode: 'history',
    scrollBehavior: () => ({y: 0})
});

/**
 * 定义根节点
 * @type {*[]}
 */
export const rootRouterMap = [
    {path: '/401', component: () => import('@/views/errorPage/401'), meta: {needAuth: false}},
    {path: '/404', component: () => import('@/views/errorPage/404'), meta: {needAuth: false}},
    {path: '/500', component: () => import('@/views/errorPage/500'), meta: {needAuth: false}},
    {path: '/', component: () => import('@/views/Layout/Layout'), children : menuRouterMap, redirect:'/Index'},
    {path: '*', redirect: '/404'}
];

vueRouter.beforeEach(function (to, from, next) {
    //在访问页面为login时，如果判断已经登录，则直接访问首页，无需再次登录
    // if(to.path === '/login'){
    //   if (vueRouter.app.$options.store.getters.isLogin) {
    //     next({ path: '/Index'});
    //   }else{
    //     next();
    //   }
    // }
    // //根据字段判断是否路由过滤
    // else if (to.matched.some(record => record.meta.needAuth)) {
    //   //使用router实例，在router.app.$options.store中包含了dispatch、mutations和state、getters
    //   if (vueRouter.app.$options.store.getters.isLogin) {
    //     //已登录的，正常的路由访问
    //     next();
    //   } else {
    //     // next();
    //     //未登录的，访问登录页面
    //     next({  path: '/login'  });
    //   }
    // }
    // //既不是login页面，又不是需要判断登录的页面，就直接跳转
    // else {
    //   next();
    // }

    next();
});

vueRouter.addRoutes(rootRouterMap);

export default vueRouter;
